package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.RoomTypeDao;
import com.aaa.javaweb.entity.RoomType;
import com.aaa.javaweb.entity.Roomtypelike;
import com.aaa.javaweb.until.JDBCUntil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


@Repository
public class RoomTypeDaoImpl implements RoomTypeDao {


    @Override
    public List<Map> selectAll(Roomtypelike roomtypelike) {
        String sql ="SELECT t_room.id,t_room.roomcode,t_room.status,t_type.tname  from t_room JOIN t_type ON t_room.typeid=t_type.id  where 1=1";
        if (roomtypelike.getSellike()!=null && roomtypelike.getSellike()!=""){
            sql+=roomtypelike.getSellike();
        }
        sql+=" limit ?,?";
        int start = roomtypelike.getLimit()*(roomtypelike.getPage()-1);
        System.out.println(sql);
        return JDBCUntil.selectMap(sql,start,roomtypelike.getLimit());
    }

    @Override
    public int delone(int id) {
        String sql = "delete FROM t_room  WHERE id=?;";
        return JDBCUntil.Updateout(sql,id);
    }

    @Override
    public List<Map> selroomtype() {
        String sql ="select id,tname from t_type";
        return JDBCUntil.selectMap(sql);
    }

    @Override
    public int tianjia(RoomType room) {
        if (room.getId()==null){
            String sql="insert into t_room (roomcode,status,typeid) values(?,?,?)";
            return JDBCUntil.Updateout(sql,room.getRoomcode(),room.getStatus(),room.getTypeid());
        }
        else {
            String sql = "update t_room set roomcode=?,status=?,typeid=? where id=?";
            return JDBCUntil.Updateout(sql,room.getRoomcode(),room.getStatus(),room.getTypeid(),room.getId());
        }
    }


    @Override
    public int selcount() {
        String sql ="SELECT count(*) as a from t_room JOIN t_type ON t_room.typeid=t_type.id";
        return Integer.parseInt(JDBCUntil.selectMap(sql).get(0).get("a")+"");
    }

    @Override
    public List<Map> selroomcode() {
        String sql ="select roomcode from t_room";
        return JDBCUntil.selectMap(sql);
    }


}
